package org.example;/*
 * title:
 * author:
 * date:
 * description:
 * */

import java.util.Scanner;

//问题是数组不能重复使用，满了之后再添加就不行的，一次性的
//没有取模，没有环形队列
public class ArrayQueue {
    public static void main(String[] args) {
        ArrayQ arrayQ = new ArrayQ(3);
        char key = ' ';
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        while (loop) {
            System.out.println("s显示队列");
            System.out.println("e退出程序");
            System.out.println("a添加数据到队列");
            System.out.println("g从队列取出数据");
            System.out.println("h查看队列头信息");
            key = scanner.next().charAt(0);
            switch (key) {
                case 's':
                    arrayQ.showQueue();
                    break;
                case 'a':
                    System.out.println("输出一个数");
                    int i = scanner.nextInt();
                    arrayQ.addQueue(i);
                    break;
                case 'g':
                    try {
                        int res = arrayQ.getQueue();
                        System.out.println("获取到的第一个数据是：" + res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int head = arrayQ.showHead();
                        System.out.println("队列头的数据是：" + head);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序结束");
    }
}

class ArrayQ {
    private int maxSize;
    private int front; //头
    private int rear;  //尾
    private int[] arr;

    //构造器
    public ArrayQ(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
        front = -1; //头的前一个
        rear = -1;  //尾，包含尾
    }

    public boolean isFull() {
        return rear == maxSize - 1;
    }

    public boolean isEmpty() {
        return rear == front;
    }

    public void addQueue(int n) {
        if (isFull()) {
            System.out.println("满了");
            return;
        }
        rear++;
        arr[rear] = n;
    }

    public int getQueue() {
        if (isEmpty()) {
            throw new RuntimeException("空的");
        }
        front++;
        int temp = arr[front];
        int[] newArray = new int[arr.length];
        for (int i = front + 1; i <= rear; i++) {
            newArray[i - 1] = arr[i];
        }
        arr = newArray;
        return temp;
    }

    //遍历队列
    public void showQueue() {
        if (isEmpty()) {
            System.out.println("空的");
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }

    public int showHead() {
        if (isEmpty()) {
            System.out.println("空的");
            throw new RuntimeException("空的");
        }
        return arr[front + 1];
    }
}

class ArrayCircleQ {
    private int maxSize;
    private int front; //头
    private int rear;  //尾
    private int[] arr;

    //构造器
    public ArrayCircleQ(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
        front = -1; //头的前一个
        rear = -1;  //尾，包含尾
    }

    public boolean isFull() {
        return rear == maxSize - 1;
    }

    public boolean isEmpty() {
        return rear == front;
    }

    public void addQueue(int n) {
        if (isFull()) {
            System.out.println("满了");
            return;
        }
        rear++;
        arr[rear] = n;
    }

    public int getQueue() {
        if (isEmpty()) {
            throw new RuntimeException("空的");
        }
        front++;
        int temp = arr[front];
        int[] newArray = new int[arr.length];
        for (int i = front + 1; i <= rear; i++) {
            newArray[i - 1] = arr[i];
        }
        arr = newArray;
        return temp;
    }

    //遍历队列
    public void showQueue() {
        if (isEmpty()) {
            System.out.println("空的");
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }

    public int showHead() {
        if (isEmpty()) {
            System.out.println("空的");
            throw new RuntimeException("空的");
        }
        return arr[front + 1];
    }
}
